/* 2 */
/*
var argv = process.argv;
var sum = 0;

for (var i = 0; i < argv.length; i++) {
	if (i >1)
		sum += Number(argv[i]);
}
console.log(sum);
*/

/* 3 */
/*
var fs = require("fs");

var content = fs.readFileSync(process.argv[2]);
var lines = content.toString().split("\n").length - 1;

console.log(lines);
*/

/* 4 */
/*
var fs = require("fs");

fs.readFile(process.argv[2], "utf-8", function callback(err, data) {
	if (err) return ;

	var lines = data.toString().split("\n").length - 1;
	console.log(lines);
});
*/

/* 5 */
/*
var fs = require("fs");
var path = require("path");

fs.readdir(process.argv[2], function callback(err, list) {
	list.forEach(function each(file) {
		if (path.extname(file) === "." + process.argv[3])
			console.log(file);
	})
})
*/

/* 6 */
/*
var readdir = require("./mymodule");
readdir(process.argv[2], process.argv[3], function(err, list) {
	if (err)
		return console.error("There was an error:", err);

	list.forEach(function each(file){
		console.log(file);
	})
});
*/

/* 7 */
/*
var http = require("http");

http.get(process.argv[2], function callback(res) {
	res.setEncoding("utf-8");

	res.on("data", function(data) {
		console.log(data);
	})

	res.on("error", function(err) {
		console.error(err);
	})
})*/

/* 8 */
/*var http = require("http");
var bl = require("bl");

http.get(process.argv[2], function callback(res) {
	res.pipe(bl(function(err, data){
		if (err)
			console.error(err);
		data = data.toString();
		console.log(data.length);
		console.log(data);
	}))
})*/

/* 9 */
/*
var http = require("http");
var bl = require("bl");

var results = [];
var count = 0;

function printResults () {
	for (var i = 0; i < 3; i++)
		console.log(results[i]);
}

function httpGet(index) {
	http.get(process.argv[2 + index], function (response) {
		response.pipe(bl(function (err, data) {
			if (err)
				return console.error(err);

			results[index] = data.toString();
			count++;

			if (count == 3)
				printResults();

		}))
	})
}

for (var i = 0; i < 3; i++)
	httpGet(i);
*/

/* 10 */
/*
var net = require("net");
var strftime = require("strftime");

function zeroFill(i) {
	return (i < 10 ? "0" : "") + i;
}

function now() {
	var d = new Date();

	// Method 1
	if (true)
		return strftime("%F %H:%M", d);

	// Method 2
	return d.getFullYear() + "-"
		+ zeroFill(d.getMonth() + 1) + "-"
		+ zeroFill(d.getDate()) + " "
		+ zeroFill(d.getHours()) + ":"
		+ zeroFill(d.getMinutes());
}

var server = net.createServer(function (socket) {
	socket.write(now() + "\n");
	socket.end();
})

server.listen(Number(process.argv[2]));
*/

/* 11 */
/*
var http = require("http");
var fs = require("fs");

var server = http.createServer(function(req, res) {
	res.writeHead(200, {"content-type": "text/plain"});

	fs.createReadStream(process.argv[3]).pipe(res);
})

server.listen(Number(process.argv[2]));
*/

/* 12 */
/*
var http = require("http");
var map = require("through2-map");

http.createServer(function(req, res) {
	if (req.method != "POST")
		return res.end("send me a POST\n");

	req.pipe(map(function (chunk) {
		return chunk.toString().toUpperCase();
	})).pipe(res);

}).listen(Number(process.argv[2]));
*/

/* 13 */

var http = require("http");
var url = require("url");

function parseTime(time) {
	return {
		hour: time.getHours(),
		minute: time.getMinutes(),
		second: time.getSeconds()
	}
}

function unixTime(time) {
	return {unixtime: time.getTime()};
}

var server = http.createServer(function(req, res) {
	var parsedUrl = url.parse(req.url, true);
	var time = new Date(parsedUrl.query.iso);

	var result;

	if (/^\/api\/parsetime/.test(req.url)) {
		result = parseTime(time);
	} else if (/^\/api\/unixtime/.test(req.url)) {
		result = unixTime(time);
	}

	if (result) {
		res.writeHead(200, {"Content-type" : "application/json"});
		res.end(JSON.stringify(result));
	} else {
		res.writeHead(404);
		res.end();
	}

})

server.listen(Number(process.argv[2]));
